package c17网络编程;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class 多线程ClientSocket {

	public static void main(String[] args) {
		try {
			InetAddress addr = InetAddress.getByName(null);
			//客户端socket
			Socket socket = new Socket(addr, 多线程ServerSocket.SERVER_PORT);
			//读取服务端信息的输入流
			BufferedReader in = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));
			//发信息给服务端的输出流
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())), true);
			//读取控制台输入的输入流
			Scanner console = new Scanner(System.in);

			//服务端信息
			String serverMsg = null;
			//循环监听控制台输入并发送信息
			while ((serverMsg = in.readLine()) != null) {
				if (!"success".equals(serverMsg)) {
					System.err.println(serverMsg);
				}
				//一次读入一行
				String input = console.nextLine();
				//退出循环结束线程
				if ("end".equals(input)) {
					break;
				}
				out.println(input);
//				out.flush(); //注意：上面如果用print，这里还需要调用这个方法清空缓存才能成功发送信息！
				System.err.println("已发送到服务端，请输入下一行：");
			}

			//关闭所有资源
			socket.close();
			in.close();
			out.close();
			console.close();
		} catch (IOException e) {
			System.err.println("服务器已关闭...");
		}

	}

}
